<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\goods\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Cache;
use think\Db;
use app\goods\model\Crowd as CrowdModel;

/**
 * 人群
 * @package app\admin\controller
 */
class Crowd extends Admin
{
    /**
    *列表
    */


    public function index(){
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $order = $this->getOrder('sort ASC,crowdid DESC');
        $data_list = CrowdModel::order($order)->select();       
        return ZBuilder::make('table')
        ->addOrder('sort,crowdid')
        ->addColumns([ // 批量添加列
            ['sort', '排序'],
            ['crowdid', 'ID'],
            ['crowdname', '名称'],
            ['status', '状态' , 'switch'],
            ['right_button', '操作', 'btn']
        ])
        ->setTableName('goods_crowd')
        ->setPrimaryKey('crowdid')
        ->addRightButton('edit')
        ->addTopButton('add')
        ->addRightButton('delete')
        ->setRowList($data_list)
        ->fetch();
    }


    /**
    *新增
    */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = CrowdModel::insert($data);
            if($result) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }


        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列            
            ['text', 'crowdname', '名称'],
            ['text', 'sort', '排序'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->layout(['crowdname' => 6, 'status' => 6, 'sort' => 6, 'thumb' => 6,])
        ->fetch();
    }

    /**
    *修改
    */
    public function edit($id = 0){
        if ($id === 0) return $this->error('缺少参数');

        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = CrowdModel::update($data);
            if($result) {
                $this->success('修改成功', cookie('__forward__'));
            }else{
                $this->error('修改失败');
            }
        }

        //详情
        $info      = CrowdModel::where('crowdid',$id)->find();

        return ZBuilder::make('form')
        ->addFormItems([ // 批量添加列 
            ['hidden', 'crowdid'],           
            ['text', 'crowdname', '名称'],
            ['text', 'sort', '排序'],
            ['radio', 'status' , '状态', '', ['0' => '禁用' ,'1' => '启用'], '1'],            
            
        ])
        ->setFormData($info)
        ->layout(['crowdname' => 6, 'status' => 6, 'sort' => 6, 'thumb' => 6,])
        ->fetch();
    }


    /**
    *删除
    */
    public function delete($ids = 0){
        $id     = input('ids');
        $result = CrowdModel::where(array('crowdid' => $id))->delete();
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }   

    
   
}
